package com.boilerCloud.dao.sys.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.boilerCloud.dao.BaseDao;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.sys.ProtocolInfo;
import com.boilerCloud.util.StringUtil;
@Repository
public class ProtocolDao extends BaseDao {

	public void qryList(ProtocolInfo model, HzlqswPage<ProtocolInfo> page)throws Exception {

		String countsql = "select count(*) from protocol_info a "
				+ " where a.is_delete='n' ";
			
		String listSql = "select a.* "
					+ " from protocol_info a "
					+ " where a.is_delete='n' ";
		List<Object> params= new ArrayList<Object>();
		if(!StringUtil.isEmpty(model.getName())){
			countsql += " and a.name like ?";
			listSql += " and a.name like ?";
			params.add("%" + model.getName() +"%");
		}
		
			
		listSql += " order by id desc ";
		this.queryListByPage(countsql, params, listSql, params, page, ProtocolInfo.class);
	}

	public void save(ProtocolInfo model) {
		String sql = "insert into protocol_info(name,account,secret_key,visit_address,"
				+ "description,usable,create_time,modify_time,is_delete) "
				+ "values(?,?,?,?,?,?,now(),now(),'n')";
			
		List<Object> param = new ArrayList<Object>();
		
		param.add(model.getName());
		param.add(model.getAccount());
		param.add(model.getSecretKey());
		param.add(model.getVisitAddress());
		param.add(model.getDescription());
		param.add(model.getUsable());
		this.jdbcTemplate.update(sql,param.toArray());
		
	}

	public void edit(ProtocolInfo model) throws Exception{
		String sql = "update protocol_info set name=?,account=?,secret_key=?,visit_address=?,"
				+ "description=?,usable=?,modify_time=now() where id=?";
		List<Object> param = new ArrayList<Object>();
		
		param.add(model.getName());
		param.add(model.getAccount());
		param.add(model.getSecretKey());
		param.add(model.getVisitAddress());
		param.add(model.getDescription());
		param.add(model.getUsable());
		param.add(model.getId());
		this.jdbcTemplate.update(sql,param.toArray());
		
	}

	public void del(int id)throws Exception {
		String sql = "update protocol_info set is_delete='y' where id=?";
		this.jdbcTemplate.update(sql,id);
		
	}

	public ProtocolInfo getProtocolInfo(int id)throws Exception {
		String sql = "select * from protocol_info where id =? limit 1";
		
		return this.queryForObject(sql, ProtocolInfo.class, id);
	}



}
